package mge.ui 
{
  import com.bit101.components.PushButton;
  import flash.display.DisplayObjectContainer;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.KeyboardEvent;
  import flash.text.StyleSheet;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.ui.Keyboard;

  
	/**
   * ...
   * @author ivan
   */
  public class ModalDialog 
  {
    
    
    public static const RESULT_YES:int = 0;
    public static const RESULT_NO:int = 1;
    public static const RESULT_CANCEL:int = 2;
    
    public var result:int = -1;
    
    /*
     * Modal dialog with optional buttons that gets focus when instantiated.
     * Optional button names enable/disable those buttons too.
     * onDialogEnd is called with the result of the button pressed as parameter.
     * 
     * @parent
     * @text        text to be shown
     * @style       StyleSheet used by text
     * @acceptText  Text for button Accept/Yes/Ok
     * @onDialogEnd Callback called with  the result of the button pressed as parameter.
     * @noText      Text for button No
     * @cancelText  Text for button Cancel
     * */
    public function ModalDialog(parent:DisplayObjectContainer, text:String, style:StyleSheet,
                                acceptText:String = "OK", onDialogEnd:Function = null,
                                noText:String = null, cancelText:String = null)
    {
      this.parent = parent;
      this.onDialogEnd = onDialogEnd;
      
      // Adds a dark background that prevents childrens to get input
      background = new Sprite();
      background.graphics.beginFill(0x000000, 0.5);
      background.graphics.drawRect(0, 0, parent.width, parent.height);
      background.graphics.endFill();
      parent.addChild(background);
      
      // Adds a panel
      panel = new Sprite();
      background.addChild(panel);
      buttonContainer = new Sprite()
      panel.addChild(buttonContainer);
      // panel.alpha = 0.5;
            
      // Textfield stuff
      textField.autoSize = TextFieldAutoSize.LEFT;
      textField.multiline = true;
      if (style) textField.styleSheet = style;
      textField.selectable = false;
      textField.htmlText = text;
      textField.mouseEnabled = false;
      panel.addChild(textField);
      
      // Adds button(s)
      var okButton:PushButton = new PushButton(buttonContainer, 0, 0, acceptText, onButtonOk);
      var noButton:PushButton;
      var cancelButton:PushButton;
      if (noText != null)
        noButton = new PushButton(buttonContainer, 0, 0, noText, onButtonNo);
      if (cancelText != null)
        cancelButton = new PushButton(buttonContainer, 0, 0, cancelText, onButtonCancel);
        
      // Repositions buttons as needed.
      if (noButton && cancelButton)
      {
        okButton.x = 300;
        noButton.x = 180;
      }
      else if (noButton)
        okButton.x = 120;
        
      // Panel position depends on buttons width
      var w:Number = Math.max(textField.width + 20, buttonContainer.width + 20);
      var h:Number = textField.height + buttonContainer.height + 30;
      
      panel.graphics.beginFill(0xCCCCCC, 0.5);
      panel.graphics.drawRoundRect(0, 0, w, h, 20, 20);
      panel.graphics.endFill();
      
      // Button position depends on panel :)
      buttonContainer.x = panel.width / 2 - buttonContainer.width / 2;
      buttonContainer.y = panel.height - buttonContainer.height - 10;
      
      // Textfield is cenetered in panel
      textField.x = panel.width / 2 - textField.textWidth / 2;
      textField.y = panel.height / 2 - textField.textHeight / 2 - buttonContainer.height;
      
      // Panel is centered in background
      panel.x = parent.width / 2 - panel.width / 2;
      panel.y = parent.height / 2 - panel.height / 2;
      
      // Adds text
      textField.htmlText = text;
      
      // Adds a listener to capture keys.
      parent.stage.focus = okButton;
      buttonContainer.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, true);
    }
    
    private function onKeyDown(e:KeyboardEvent):void
    {
      if (e.keyCode == Keyboard.ENTER)
      {
        parent.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, true);
        onButtonOk();
      }
        
      e.stopImmediatePropagation();
    }
    
    private function clean():void
    {
      if (background)
      {
        parent.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, true);
        parent.removeChild(background);
        background = null;
        if (onDialogEnd != null) onDialogEnd(result);
      }
    }
    
    private function onButtonOk(e:Event = null):void
    {
      result = RESULT_YES;
      clean();
    }
    
    private function onButtonNo(e:Event):void
    {
      result = RESULT_NO;
      clean();
    }
    
    private function onButtonCancel(e:Event):void
    {
      result = RESULT_CANCEL;
      clean();
    }
    
    private var background:Sprite;
    private var parent:DisplayObjectContainer;
    private var panel:Sprite;
    private var buttonContainer:Sprite;
    
    private var textField:TextField = new TextField();
    private var onDialogEnd:Function;
    
  }

}